﻿using System.ComponentModel;
using System.Net.Mail;
using System.Text;

namespace QG.FrontDesk.Core
{

    public class EmailHelper
    {
        
        ///<summary>
        /// 发送邮件方法
        ///</summary>
        ///<param name="mailTo">收件人邮箱</param>
        ///<param name="mailSubject">邮件标题</param>
        ///<param name="mailContent">邮件内容</param>
        /// <summary>
        /// </summary>
        /// <param name="mailTo">收件人</param>
        /// <param name="mailSubject">主题</param>
        /// <param name="mailContent">内容</param>
        /// <param name="emailType">邮件类型</param>
        /// <param name="mailFrom">发件人</param>
        /// <param name="userPassword">授权码</param>
        /// <returns></returns>
        public static bool SendEmail(string mailTo, string mailSubject, string mailContent,string mailFrom= "xiangluzhang@foxmail.com",string userPassword = "hbebkzgosetmebcd")
        {
            string smtpServer = "";//SMTP服务器
            int port = 0;
            int emailType = 0;
            var type = mailTo.Substring(mailTo.IndexOf('@') + 1, mailTo.LastIndexOf('.') - mailTo.IndexOf('@') - 1);
            //判断收件者邮箱
            if (type.ToLower() == "qq")
            {
                emailType = 1;
            }
            else if (type == "163")
            {
                emailType = 2;
            }
            // 设置发送方的邮件信息
            switch (emailType)
            {
                case 1://QQ
                    smtpServer = "smtp.qq.com"; //SMTP服务器                 
                    smtpServer = "smtp.qq.com"; //SMTP服务器                    
                    port = 587;
                    break;
                case 2://163
                    smtpServer = "smtp.163.com"; //SMTP服务器
                    port = 25;
                    break;
                default:
                    return false;
            }

            //发件人地址
            var fromAddress = new MailAddress(mailFrom, "发件人", Encoding.UTF8);
            //收件人地址
            var toAddress = new MailAddress(mailTo, "收件人", displayNameEncoding: Encoding.UTF8);

            // 邮件服务设置
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
            smtpClient.Host = smtpServer; //指定SMTP服务器
            smtpClient.Port = port;//端口号
            smtpClient.Credentials = new System.Net.NetworkCredential(mailFrom, userPassword);//用户名和密码
            smtpClient.EnableSsl = true;//启用SSL安全发送
            smtpClient.Timeout = 30000;//设置发送失效时间为30s

            // 发送邮件设置       
            MailMessage mailMessage = new MailMessage(fromAddress, toAddress); // 发送人和收件人
            mailMessage.Subject = mailSubject;//主题
            mailMessage.Body = mailContent;//内容
            mailMessage.BodyEncoding = Encoding.UTF8;//正文编码
            mailMessage.IsBodyHtml = true;//设置为HTML格式
            mailMessage.Priority = MailPriority.High;//优先级
            

            try
            {
                smtpClient.Send(mailMessage); // 发送邮件
                //smtpClient.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);//异步发送邮件完成响应事件
                //smtpClient.SendAsync(mailMessage, mailMessage);
            }
            catch (SmtpException ex)
            {
                string a=ex.Message;
                return false;
            }
            return true;
        }


 

        //邮件发送成功后回调函数
        public static void client_SendCompleted(object sender, AsyncCompletedEventArgs e)
        {
            //UserInfo userInfo = new UserInfo();

            //if (e.Error != null )
            //{
            //    MailMessage message = new MailMessage();
            //    message.From = new MailAddress("fromAddress", "mailFrom");//发件人地址
            //    message.To.Add("fromAddress");//目标发件地址
            //    message.Subject = "神经病";//邮件标题
            //    message.Body = "密码" + userInfo.UserPwd;//邮件内容
            //        //发送邮件的smtp服务器地址
            //    SmtpClient smtp = new SmtpClient("smtp.qq.com");
            //    smtp.Credentials = new NetworkCredential("mailFrom", "userPassword");//发件人的用户名和密码
            //    smtp.Send(message);
            //}
           
        }

  

    }
}
                


        /////<summary>
        ///// 添加附件
        /////</summary>
        /////<param name="attachmentsPath">附件的路径集合，以分号分隔</param>
        //public void AddAttachments(string attachmentsPath)
        //{
        //    try
        //    {
        //        string[] path = attachmentsPath.Split(';'); //以什么符号分隔可以自定义
        //        Attachment data;
        //        ContentDisposition disposition;
        //        for (int i = 0; i < path.Length; i++)
        //        {
        //            data = new Attachment(path[i], MediaTypeNames.Application.Octet);
        //            disposition = data.ContentDisposition;
        //            disposition.CreationDate = File.GetCreationTime(path[i]);
        //            disposition.ModificationDate = File.GetLastWriteTime(path[i]);
        //            disposition.ReadDate = File.GetLastAccessTime(path[i]);
        //            //mMailMessage.Attachments.Add(data);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //        //Console.WriteLine(ex.ToString());
        //    }
        //}








